//
//  PosterCollectionView.m
//  时光电影项目
//
//  Created by 墨狂之逸才 on 15/7/24.
//  Copyright (c) 2015年 墨狂之逸才. All rights reserved.
//

#import "PosterCollectionView.h"
#import "PosterCollectionViewCell.h"
#import "MovieModal.h"
#define Kheight  [UIScreen mainScreen].bounds.size.height
#define kwidth  [UIScreen mainScreen].bounds.size.width
#import "MovieModal.h"


@implementation PosterCollectionView



- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        
        
        
        //设置代理
        self.delegate = self;
        self.dataSource = self;
//        self.pagingEnabled = YES;
        //注册
        [self registerClass:[PosterCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        
    }
    
    return self;
}



//- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
//{
//    
//    return _movieModalArray.count;
//    
//}


#pragma mark - set方法
//- (void)setMovieModalArray:(NSArray *)movieModalArray
//{
//    _movieModalArray = movieModalArray;
//    
//    [self reloadData];
//}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PosterCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    //传递具体某一个数据
    UIColor *color = [UIColor redColor];
    if (indexPath.row%2) {
        color = [UIColor greenColor];
    }
    cell.backgroundColor = color;
    MovieModal *modal = self.movieModalArray[indexPath.row];
    
    
    cell.modal = modal;
    
    
    return cell;
}


#pragma mark - 有问题？？？？不理解
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, kwidth*1/8, 0, kwidth*1/8);
}




//- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
//{
//    
////    targetContentOffset   目标偏移量
//    
//    CGFloat ItemWidth = kwidth*3/4;
//    
//    CGFloat x = targetContentOffset->x;
//    
//    NSInteger index = x/ItemWidth;
//    
//    //赋值当前下标
//    _currentIndex = index;
//    
//    targetContentOffset->x = index * ItemWidth;
//    
//}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.currentIndex == indexPath.row)
    {
        PosterCollectionViewCell *cell = (PosterCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        
        [cell flipView];
    }
    else
    {
        
        [collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        
        self.currentIndex = indexPath.row;
    }
    
   
    
}




@end
